package com.itheima.server.controller;

import com.itheima.domain.Movement;
import com.itheima.domain.Visitors;
import com.itheima.server.service.MovementService;
import com.itheima.vo.MovementsVo;
import com.itheima.vo.PageResult;
import com.itheima.vo.VisitorsVo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementController {


    @Resource
    private MovementService movementService;

    /**
     * 发布动态
     *
     * @param movement     动态
     * @param imageContent 文件
     * @return
     */
    @PostMapping
    public ResponseEntity<String> movements(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementService.movements(movement, imageContent);
        return ResponseEntity.ok("OK");
    }

    /**
     * 查看自己 动态
     *
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity<PageResult> findByUserId(Long userId, @RequestParam(defaultValue = "1") Integer page,
                                                   @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.findByUserId(userId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 查询好友动态
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> getFriendMovements(@RequestParam(defaultValue = "1") Integer page,
                                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.getFriendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 查看推荐动态
     *
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity<PageResult> recommend(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementService.recommend(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 根据id查询单条动态
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<MovementsVo> getMovementById(@PathVariable String id){
        MovementsVo movement = movementService.getMovementById(id);
        return ResponseEntity.ok(movement);
    }

    /**
     * 动态点赞
     * @param id
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Integer> like(@PathVariable String id){
        Integer count = movementService.like(id);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消点赞
     * @param id
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Integer> dislike(@PathVariable String id){
        Integer count = movementService.dislike(id);
        return ResponseEntity.ok(count);
    }

    /**
     * 喜欢
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Integer> love(@PathVariable("id") String movementId) {
        Integer likeCount = movementService.loveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 不喜欢
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Integer> unlove(@PathVariable("id") String movementId) {
        Integer likeCount = movementService.disloveComment(movementId);
        return ResponseEntity.ok(likeCount);
    }


    /**
     * 首页谁看过我
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity<List<VisitorsVo>> visitors(){
        List<VisitorsVo> vo = movementService.visitors();
        return ResponseEntity.ok(vo);
    }


}
